<template>
  <div class="registered-container">
    <el-form :model="form" :rules="rules"
             status-icon
             ref="form"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm registered-page">

      <span>
        <h3 style="display: inline-block">注册临时账号</h3>
        <el-tooltip style="margin-left: 3px" content="注意：临时用户的密码会以邮件的方式发送到邮箱中" placement="top">
           <i class="el-icon-question"></i>
        </el-tooltip>
      </span>

      <el-form-item prop="username">
        <el-input type="text"
                  v-model="form.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input type="text"
                  v-model="form.phone"
                  auto-complete="off"
                  placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input type="text"
                  v-model="form.email"
                  auto-complete="off"
                  placeholder="邮箱"
        ></el-input>
      </el-form-item>
<!--      TODO 后端分配 密码 和 角色-->
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:50%;" @click="handleSubmit">确认注册</el-button>
        <el-button type="danger" style="width:45%;" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {asyncGet, post} from "@/plugins/axios"
import {Message} from "element-ui"

export default {
  name: 'tempAccount',
  data(){
    const module = '/user'
    const check = async (rule, value, callback) => {
      const response = await asyncGet(module + '/check', {value: value})
      if (response.data.data) {
        callback()
      } else {
        callback(new Error('已存在相同数据'))
      }
    }
    return {
      uri: {
        tempAccount: module + '/tempAccount'
      },
      form: {
        username: '',
        phone: '',
        email: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 30, message: '用户名不能超过30个字符', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的手机号格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSubmit(){
      this.$refs.form.validate((valid) => {
        if(valid){
          post(this.uri.tempAccount, this.form, response => {
            if (response.code === 200) {
              Message.success('账号已成功注册，请在邮箱中查看自己账号密码')
              this.$router.push({path: '/login'})
            }
          })
        }else{
          return false
        }
      })
    },
    cancel() {
      this.$confirm('是否返回登录页?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.back();
      })
    }
  }
};
</script>

<style scoped lang="less">
.registered-container {
  width: 100%;
  height: 100%;
  .registered-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

    label.el-checkbox.rememberme {
      margin: 0 0 15px;
      text-align: left;
    }
  }
}
</style>